package cn.rkylin.oms.salespromotion.rules.util.handler;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import cn.rkylin.apollo.common.util.BeanUtils;
import cn.rkylin.oms.order.service.IOrderService;
import cn.rkylin.oms.order.vo.OrderVO;
import cn.rkylin.oms.salespromotion.rules.vo.SalesPromotionRuleVO;

/**
 * 
 * This class defines an interface for handling requests and optionally implements
 * the successor link.
 * @version 1.0
 * @created 16-08-2017 17:23:58
 */
public abstract class RuleMatchHandler {

    protected RuleMatchHandler successor;
    public static String SUCCESS = "success";
    public static String RESULT = "result";
    
    @Autowired
    protected IOrderService orderService = BeanUtils.getBean("orderService");

    public RuleMatchHandler(){

    }

    public void finalize() throws Throwable {

    }

    /**
     * 
     * @param successor
     */
    public void SetSuccessor(RuleMatchHandler successor){
        this.successor = successor;
    }

    public Map<String, String> HandleRequest(OrderVO orderVO, SalesPromotionRuleVO ruleVO) throws Exception{
        Map<String, String> mapResult = getValidMap(orderVO,ruleVO);
        if(successor!=null&&"success".equalsIgnoreCase((String) mapResult.get("result"))) {
            return successor.HandleRequest(orderVO, ruleVO);
        }else {
            return mapResult;
        }
    }
    
    public abstract Map<String,String> getValidMap(OrderVO orderVO, SalesPromotionRuleVO ruleVO) throws Exception;

}